import Promise from 'bluebird';

function baseUrl() {
  return "https://lufei.renlr.com";
}

function fdate(date, format) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  var date = null;
  if (format == "MM-DD"){
    date = [month, day].map(formatNumber).join('-');
  }else if (format == "YYYY-MM-DD") {
    date = [year, month, day].map(formatNumber).join('-');
  } else if (format == "HH:mm:ss") {
    date = [hour, minute, second].map(formatNumber).join(':');
  } else {
    date = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':');
  }
  return date;
}


function ftime(date) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  return [hour, minute].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getDateStr(date) {
  if (!date) return '';
  return date.getFullYear() + '年' + (date.getMonth() + 1) + '月' + date.getDate() + '日';
}

/**
 * 生成GUID序列号
 * @returns {string} GUID
 */
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

/**
 * 记录日志
 * @param {Mixed} 记录的信息
 * @returns {Void}
 */
function log(msg) {
  if (!msg) return;
  if (getApp().settings['debug'])
    console.log(msg);
  let logs = wx.getStorageSync('logs') || [];
  logs.unshift(msg)
  wx.setStorageSync('logs', logs)
}

/**
 * @param {Function} func 接口
 * @param {Object} options 接口参数
 * @returns {Promise} Promise对象
*/
function promiseHandle(func, options) {
  options = options || {};
  return new Promise((resolve, reject) => {
    if (typeof func !== 'function')
      reject();
    options.success = resolve;
    options.fail = reject;
    func(options);
  });
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function wxGet(opts) {
  wx.request({
    url: baseUrl() + opts.url,
    data: opts.params,
    dataType: "json",
    method: "GET",
    header: { "content-type": "application/json;charset=utf-8" },
    success: function (data) {
      if (typeof (opts.success) == "function") {
        opts.success(data);
      }
    },
    fail: function (error) {
      if (typeof (opts.fail) == "function") {
        opts.fail(data);
      }
    },
    complete: function (data) {
      if (typeof (opts.complete) == "function") {
        opts.complete(data);
      }
    }
  });
}

function wxPost(opts) {
  wx.request({
    url: baseUrl() + opts.url,
    data: JSON.stringify(opts.params),
    dataType: "json",
    method: "POST",
    header: { "content-type": "application/json;charset=utf-8" },
    success: function (data) {
      if (typeof (opts.success) == "function") {
        opts.success(data);
      }
    },
    fail: function (error) {
      if (typeof (opts.fail) == "function") {
        opts.fail(data);
      }
    },
    complete: function (data) {
      if (typeof (opts.complete) == "function") {
        opts.complete(data);
      }
    }
  });
}

//HUD 
//成功提示
function showSuccess(title = "成功啦", duration = 2500) {
  wx.showToast({
    title: title,
    icon: 'success',
    duration: (duration <= 0) ? 2500 : duration
  });
}
//loading提示
function showLoading(title = "请稍后", duration = 5000) {
  wx.showToast({
    title: title,
    icon: 'loading',
    duration: (duration <= 0) ? 5000 : duration
  });
}
//隐藏提示框
function hideToast() {
  wx.hideToast();
}

//显示带取消按钮的消息提示框
function alertViewWithCancel(title = "提示", content = "消息提示", confirm, showCancel = "true") {
  wx.showModal({
    title: title,
    content: content,
    showCancel: showCancel,
    success: function (res) {
      if (res.confirm) {
        confirm();
      }
    }
  });
}
//显示不取消按钮的消息提示框
function alertView(title = "提示", content = "消息提示", confirm) {
  alertViewWithCancel(title, content, confirm, false);
}

module.exports = {
  log: log,
  guid: guid,
  wxGet: wxGet,
  wxPost: wxPost,
  BaseURL: baseUrl,
  ftime: ftime,
  fdate: fdate,
  promiseHandle: promiseHandle,
  getDateStr: getDateStr,
  formatNumber: formatNumber,
  showSuccess: showSuccess,
  showLoading: showLoading,
  hideToast: hideToast,
  alertViewWithCancel: alertViewWithCancel,
  alertView: alertView
}